package com.gusti.turnos.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.gusti.turnos.model.User;

/**
 * 
 * @author gusti
 *
 */
public class ServerUtil {

	private static ServerUtil instance;

	public synchronized static ServerUtil getInstance() {
		if( instance == null ) {
			instance = new ServerUtil();
		}
		return instance;
	}

	private ServerUtil() {
		//do nothing
	}

	/** --------------------------------------------------
	 *  --------------------------------------------------
	 *  -------------------------------------------------- */

	private String applicationUrl;

	public String getApplicationUrl() {
		return applicationUrl;
	}

	public void setApplicationUrl(String applicationUrl) {
		this.applicationUrl = applicationUrl;
	}

	/** --------------------------------------------------
	 *  --------------------------------------------------
	 *  -------------------------------------------------- */

	public static final String CURRENT_USER = "turnos.user";

	/**
	 * 
	 * @return the logged in User
	 */
	public User getCurrentLoggedInUser() {

		HttpSession httpSession = getSession(false);
		if( httpSession != null && httpSession.getAttribute( ServerUtil.CURRENT_USER ) != null ) {
			return (User) httpSession.getAttribute( ServerUtil.CURRENT_USER );
		}
		return null;
	}

	/**
	 * 
	 * @return true if any user is logged in
	 */
	public boolean isValidSession() {

		if( getCurrentLoggedInUser() != null ) {
			return true;
		}
		return false;
	}
	/**
	 * 
	 * @return the logged in user id
	 */
	public long getCurrentUserId() {

		if( isValidSession() ) {
			return (Long) getCurrentLoggedInUser().getId();
		}
		return -1;
	}

	/**
	 * 
	 * @return the logged in user name
	 */
	public String getCurrentUserName() {

		if( isValidSession() ) {
			return (String) getCurrentLoggedInUser().getName();
		}
		return null;
	}

	/**
	 * 
	 * @return true if the logged in user is administrative user
	 */
	public boolean isCurrentUserAdmin() {

		if( isValidSession() ) {
			return (Boolean) getCurrentLoggedInUser().isAdmin();
		}
		return false;
	}

	/**
	 * 
	 * @return the logged in user homePage
	 */
	public String getCurrentUserHomePage() {

		if( isValidSession() ) {
			return (String) getCurrentLoggedInUser().getUserRol().getHomePage();
		}
		//if we have any error return an error page
		return "500";
	}

	/**
	 * 
	 * @return the logged in user uuid
	 */
	public String getCurrentUserUuid() {

		if( isValidSession() ) {
			return (String) getCurrentLoggedInUser().getUuid();
		}
		return null;
	}

	/**
	 * 
	 * @return The session identifier, if a session exists.
	 */
	public String getSessionId() {

		HttpSession httpSession = getSession(false);
		if( httpSession != null ) {
			return httpSession.getId();
		}
		return null;
	}

	/**
	 * Helper method that gets the current session.
	 */
	public HttpSession getSession( boolean create ) {

		HttpServletRequest request = null;

		ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();

		if( servletRequestAttributes != null ) {
			request = servletRequestAttributes.getRequest();
			if( request != null ) {
				return request.getSession( create );
			}
		}
		return null;
	}
}
